package com.ingenio.siot.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ingenio.siot.adapter.AlmacenarObservacionesAdapter;

/**
 * Servlet implementation class AlmacenarObservacionesServlet
 */
@WebServlet("/almacenarObservaciones")
public class AlmacenarObservacionesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AlmacenarObservacionesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("hola2");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			String idMovil = request.getParameter("id_movil");
			String latitud = request.getParameter("lat");
			String longitud = request.getParameter("lng");
			String velocidad = request.getParameter("velocidad");
					
			if(isInteger(idMovil) && isDouble(latitud) && isDouble(longitud) && isDouble(velocidad)){
			  Integer id_movil = Integer.parseInt(request.getParameter("id_movil"));
			  Double lat = Double.parseDouble(request.getParameter("lat"));
			  Double lng = Double.parseDouble(request.getParameter("lng"));
			  Double vel = Double.parseDouble(request.getParameter("velocidad"));
			  String fecha = request.getParameter("fecha");
			  AlmacenarObservacionesAdapter aa = new AlmacenarObservacionesAdapter();
			  aa.almacenarObservacion(id_movil, lat, lng, vel, fecha);
			}
		}
		catch(Exception e){
			
		}
	}
	
	private static boolean isInteger(String cadena){
		try {
			Integer.parseInt(cadena);
			return true;
		} catch (NumberFormatException nfe){
			return false;
		}
	}
	
	private static boolean isDouble(String cadena){
		try {
			Double.parseDouble(cadena);
			return true;
		} catch (NumberFormatException nfe){
			return false;
		}
	}
	

}
